package com.empty_pro.newone;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyNetWork1 implements Runnable {
    private Handler handler;
    private static final String TAG = "MyNetWork1";

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public static void main(String[] args) {

        Thread t = new Thread();
        t.start();
    }

    @Override
    public void run() {
        //URL url = null;
        //url = new URL("www.usd-cny.com/bankofchina.htm");
        //https://www.boc.cn/sourcedb/whpj/
        // 解析数据
        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        //List<String> list=new ArrayList<String>();
        String url = "https://www.boc.cn/sourcedb/whpj/";
        List<String> list = new ArrayList<String>();
        try {
            Document doc = Jsoup.connect(url).get();

            String html= doc.html();
            //Log.i(TAG, "run: 解析结果日志输出"+html);

            Element table = doc.getElementsByTag("table").get(1);
            Elements tds = table.getElementsByTag("td");

            for (int i = 0; i < tds.size(); i += 8) {
                Element td1 = tds.get(i);
                Element td2 = tds.get(i + 5);
                /*
                HashMap<String,String> map=new HashMap<String,String>();
                map.put("key",td1.text());
                map.put("val",td2.text());
                listItems.add(map);
                   */

                list.add(td1.text()+"==>"+td2.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //将参数传回主线程
        Message msg = handler.obtainMessage(5, list);
        handler.sendMessage(msg);
        //
    }
}
